package com.tjshop.android.view;

import java.util.ArrayList;
import java.util.List;

import com.tjshop.android.R;
import com.tjshop.android.model.CouponHomeData;
import com.tjshop.android.model.keyPair;
import com.tjshop.android.view.CategoryPopWindow.MyAdapter;
import com.tjshop.android.view.CategoryPopWindow.OnClickListener;
import com.tjshop.android.view.CategoryPopWindow.ViewHolder;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.PaintDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class UseCouponPopWindow extends PopupWindow {
	private Context mContext;
	private OnClickListener clickListener;
	private View contentView;

	public UseCouponPopWindow(Context context, String[] shops,
			final OnClickListener listener, String id, String price) {
		this.mContext = context;
		LayoutInflater inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		contentView = inflater.inflate(R.layout.popupwindow_for_usercoupon,
				null);
		// 设置SelectPicPopupWindow的View
		this.setContentView(contentView);
		setFocusable(true);
		WindowManager wm = (WindowManager) context
				.getSystemService(Context.WINDOW_SERVICE);
		int width = wm.getDefaultDisplay().getWidth();
		int height = wm.getDefaultDisplay().getHeight();
		this.setWidth(width - 20);
		this.setHeight(height / 3);
		// this.setFocusable(true);
		this.clickListener = listener;
		this.setOutsideTouchable(true);
		this.setBackgroundDrawable(new PaintDrawable());
		this.update();
		final Spinner shop = (Spinner) contentView.findViewById(R.id.shops);

		TextView tv_code = (TextView) contentView.findViewById(R.id.ids);
		TextView tv_price = (TextView) contentView.findViewById(R.id.price);
		tv_code.setText("交易码:" + id);
		tv_price.setText("本单享受vip价：" + price + "元");
		// 将可选内容与ArrayAdapter连接起来
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(mContext,
				android.R.layout.simple_spinner_item, shops);

		// 设置下拉列表的风格
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

		// 将adapter 添加到spinner中
		shop.setAdapter(adapter);

		// 设置默认值
		shop.setVisibility(View.VISIBLE);
		final EditText veri = (EditText) contentView.findViewById(R.id.veri);
		Button verify = (Button) contentView.findViewById(R.id.verify);

		verify.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View arg0) {
				if (clickListener != null) {
					int index = shop.getSelectedItemPosition();
					String veriCode = veri.getText().toString();
					if (index == 0) {
						Toast.makeText(mContext, "请选择商家", Toast.LENGTH_SHORT)
								.show();
						return;
					}
					if (veriCode.equals("")) {
						Toast.makeText(mContext, "请输入商家验证密码",
								Toast.LENGTH_SHORT).show();
						return;
					}
					dismiss();
					clickListener.onClickUse(index, veriCode);
				}
			}
		});
		// this.setAnimationStyle(R.style.AnimationFade);

	}

	public static interface OnClickListener {
		public void onClickUse(int shopIndex, String veriCode);
	}

	/**
	 * 显示popupWindow
	 * 
	 * @param parent
	 */
	@SuppressLint("NewApi")
	public void showPopupWindow(View parent) {
		if (!this.isShowing()) {
			int[] location = new int[2];
			parent.getLocationOnScreen(location);
			this.showAtLocation(parent, Gravity.NO_GRAVITY, location[0] + 10,
					location[1] + parent.getHeight());
		} else {
			this.dismiss();
		}
	}
}
